﻿using System;
using System.Globalization;
using System.Windows.Data;
using Bricks.Injecting;
using Bricks.Logging;

namespace Motar.Desktop.Converters
{

    /// <summary>
    /// 
    /// </summary>
    public class NegateBooleanConverter : IValueConverter
    {

        /// <summary>
        /// 
        /// </summary>
        public NegateBooleanConverter() :
            this(new SingleValueResolver<ILoggerFacade, NullLoggerFacade>())
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="loggerFacadeResolver"></param>
        protected NegateBooleanConverter(IValueResolver<ILoggerFacade> loggerFacadeResolver)
        {
            if(loggerFacadeResolver == null)
            {
                throw new ArgumentNullException("loggerFacadeResolver");
            }
            _loggerFacadeResolver = loggerFacadeResolver;
        }

        /// <summary>
        /// 
        /// </summary>
        private readonly IValueResolver<ILoggerFacade> _loggerFacadeResolver;

        /// <summary>
        /// 
        /// </summary>
        protected virtual IValueResolver<ILoggerFacade> LoggerFacadeResolver
        {
            get { return _loggerFacadeResolver; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                return !(bool)value;
            }
            catch(Exception exception)
            {
                try
                {
                    ILoggerFacade loggerFacade = LoggerFacadeResolver.Resolve();

                    loggerFacade.Debug(exception);
                }
                catch { }

                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
